/**
 * 
 */


$(function() {

	var all_price = $('.all_price');
	var sum = 0.0;
	$('.price').each(function() {
				sum = sum + parseFloat($(this).html());
			})
	all_price.html(parseFloat(sum));

	$('#div_pay').dialog({
				autoOpen : false,
				title : '支付订单',
				closeText : '关闭',
				width : 500,
				height : 550,
				resizable : false,
				modal : true,
				show : 'slide',
				hide : 'slide',
				buttons : {
					'<<继续购物' : function() {
						location.href = '/index'
					},
					'查看订单' : function() {
						location.href = '/user/usercenter/mydata'
					}
				},
				beforeClose : function() {
					if (confirm("确定要关闭支付窗口吗")) {
						location.href = '/user/usercenter/mydata';
					} else {
						return false;
					};
				}
			});
	$('#infor_form').validate({

		rules : {
			conPersonname : {
				required : true,
				minlength : 2
			},
			conPhone : {
				required : true,
				minlength : 11
			},
			conEmail : {
				email : false,
				minlength : 6
			}

			,
			tra1Personname : {
				required : true,
				minlength : 2
			},
			tra1Idcard : {
				required : true,
				minlength : 18
			},
			tra1Country : {
				required : true,
				minlength : 2
			},
			tra1Phone : {
				required : true,
				minlength : 11
			}

			/*
			 * ,tra2Personname:{ minlength:2 } ,tra2Idcard:{ minlength:18 }
			 * ,tra2Country:{ minlength:2 } ,tra2Phone:{ minlength:11 }
			 * 
			 * ,address_name:{ required:true,minlength:2 } ,address_phone:{
			 * required:true,minlength:11 } ,address:{
			 * required:true,minlength:10 }
			 */
		},
		messages : {
			conPhone : {
				minlength : '请输入有效合法的手机号码'
			},
			tra1Phone : {
				minlength : '请输入有效合法的手机号码'
			},
			tra1Idcard : {
				minlength : '请输入有效合法身份证号码'
			},
			conEmail : {
				minlength : "请输入有效邮箱"
			}
			/*
			 * ,address_phone:{ minlength:'请输入有效合法的手机号码' } ,address:{
			 * minlength:'请输入详细的收货地址' }
			 */
		},
		highlight : function(element, errorClass) {
			$(element).removeClass('succ').css('border', ' 1px solid red');
		},
		unhighlight : function(element, errorClass) {
			$(element).addClass('succ').css('border', '1px solid #B0B0B0');
		},

		success : function() {

		},
		submitHandler : function(form) { // 是否验证成功
			// 这里直接验证成功后 直接提交订单，而不是通过按钮点击提交！！如果在加上点击，那么要点击2次
			$('#div_pay').dialog('open');

			var pri = $('.all_price').html();
			// 获取product_id数组
			var arr_id = new Array();
			var index = 0;
			$('.pro_id').each(function() {
						arr_id[index] = $(this).html();
						index++;
					})
			// 获取quantity数组
			var arr_quantity = new Array();
			var dex = 0;
			$('.quan').each(function() {
						arr_quantity[dex] = $(this).html();
						dex++;
					})
			// 获取target_date数组
			var arr_date = new Array();
			var inde = 0;
			$('.target_date').each(function() {
						arr_date[inde] = $(this).html();
						inde++;
					})

			var formdata = $("#infor_form").serializeArray();
			var d = {};
			$.each(formdata, function() {
						d[this.name] = this.value;
					})

			$.ajax({
						type : "post",
						url : "/user/topay",
						data : {
							product_id : arr_id,
							pay_price : pri,
							quantity : arr_quantity,
							target_date : arr_date,
							str : JSON.stringify(d)
						},
						dataType : "json",
						ContentType : "application/json;charset=UTF-8",

						async : true, // 设置为异步
						traditional : true, // 传递数组必须要设置这个属性
						beforeSend : function() {
							$('#div_pay')
									.html("<br><h2>正在请求支付中....</h2><br><br>")
						},
						success : function(response) {
							if (response.oderId != null) {
								$('#queryoder').html(response.oderId);
								$('#div_pay')
										.html("<br><h2>请用支付宝扫码支付</h2><br><br>");
								jQuery('#div_pay').qrcode(response.qrcode);
								jquery('#div_pay').qrcode({
											width : 164,
											height : 164
										});
							}
							if (response.erro != null) {
								$('#div_pay').html("支付出错，请稍后再试")
							}

						},
						error : function() {
							$('#div_pay').html("<br><h2>系统繁忙，请稍后再试</h2><br>")
						}
					})

			timer = setInterval(queryoderstatus, 2000)
		}
	});
});

var timer = null;
function queryoderstatus() {
	var oid = $('#queryoder').html();
	$.ajax({
		type : "post",
		url : "/user/QueryOderStatus",
		data : {
			oderId : oid
		},

		async : true, // 设置为异步
		beforeSend : function() {
		},
		success : function(data) {
			if (data == "true") {
				$('#div_pay')
						.html("<br><br><br><br><br><h2>您已成功支付订单</h2><br><img alt=成功支付  src=/resources/login_img/succpay.jpg>");
				clearInterval(timer);
			}
		},
		error : function() {
		}
	})
}
